package com.huaxin.hxcloud.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;

/**
 * @author lwjing
 * @date 2018/4/12
 * @Desc $desc$
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class MessageCodeCheckDialog extends Dialog implements View.OnClickListener {

    private TextView       tv_code_title;
    private TextView       tv_des_code;
    private EditText       et_msg_code;
    private TextView       tv_tiem_count;
    private CountDownTimer codeTimer;
    private TextView       tv_cancle;
    private TextView       tv_ensure;
    private DialogCallBack callBack;

    public MessageCodeCheckDialog(@NonNull Context context) {
        super(context);
    }

    public MessageCodeCheckDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.Translucent_NoTitle);
    }

    protected MessageCodeCheckDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_message_code);
        initView();
    }

    /**
     *取消倒计时
     */
    public void timerCancle() {
        if (codeTimer != null) {
            codeTimer.cancel();
        }
        tv_tiem_count.setText("重新获取");
        tv_tiem_count.setClickable(true);
    }

    public void startTimer(){
        codeTimer.start();
        //开启倒计时后，不可点击
        tv_tiem_count.setClickable(false);
    }

    public void setNumberAndName(String phoneNumer,String valueName){
        tv_code_title.setText("是否确认修改"+valueName);
        tv_des_code.setText("验证码已发送至");
    }

    private void initView() {
        tv_code_title = (TextView) findViewById(R.id.tv_title);
        tv_des_code = (TextView) findViewById(R.id.tv_des);
        et_msg_code = (EditText) findViewById(R.id.et_msg_code);
        tv_tiem_count = (TextView) findViewById(R.id.tv_time_count);
        tv_cancle = (TextView) findViewById(R.id.tv_cancel);
        tv_ensure = (TextView) findViewById(R.id.tv_ensure);
        tv_code_title.setText("是否确认修改");
        tv_des_code.setText("验证码已发送至");
        tv_tiem_count.setText("获取验证码");

        //倒计时结束
        codeTimer = new CountDownTimer(90 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_tiem_count.setText("还剩" + millisUntilFinished / 1000 + "秒");
            }

            @Override
            public void onFinish() {
                //倒计时结束
                tv_tiem_count.setText("重新获取");
                tv_tiem_count.setClickable(true);
            }
        };

        tv_tiem_count.setOnClickListener(this);

        tv_cancle.setOnClickListener(this);

        tv_ensure.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_time_count:
                //获取验证码

                if (callBack != null) {
                    callBack.getCode();
                }
                break;
            case R.id.tv_cancel:
                if (callBack != null) {
                    callBack.cancle();
                }
                break;
            case R.id.tv_ensure:
                String msg_code = et_msg_code.getText().toString().trim();
                if (TextUtils.isEmpty(msg_code)) {
                    ToastUtil.showToast("请填写验证码");
                } else {
                    if (callBack != null) {
                        callBack.ensure(msg_code);
                    }
                    dismiss();
                }
                break;
        }
    }

    public void setDialogCallBack(DialogCallBack callBack) {
        this.callBack = callBack;
    }

    public interface DialogCallBack {
        void getCode();

        void cancle();

        void ensure(String code);
    }
}
